﻿using System;
using System.Linq;
using System.ComponentModel;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using Microsoft.Phone.Shell;
using SMSAssistant.Model;


namespace SMSAssistant
{
    public class MainViewModel : INotifyPropertyChanged
    {

        MessageDataContext msgDB;

        public bool AutoNavigate
        {
            get
            {
                return (bool)PhoneApplicationService.Current.State["AUTO_NAVIGATE"];
            }
        }

        Message selectedMessage;

        public Message SelectedMessage
        {
            get { return selectedMessage; }
            set
            {
                selectedMessage = value;
                NotifyPropertyChanged("SelectedMessage");
            }
        }

        public MainViewModel()
        {
            this.Items = new ObservableCollection<ItemViewModel>();

        }

        /// <summary>
        /// A collection for ItemViewModel objects.
        /// </summary>
        public ObservableCollection<ItemViewModel> Items { get; private set; }

        public bool IsDataLoaded
        {
            get;
            set;
        }

        /// <summary>
        /// Creates and adds a few ItemViewModel objects into the Items collection.
        /// </summary>
        public void LoadData(string searchText = "")
        {
            this.Items.Clear();
            msgDB = new MessageDataContext(MessageDataContext.DBConnectionString);
            IQueryable<Message> messages;
            if (string.IsNullOrEmpty(searchText))
            {
                messages = msgDB.Messages.OrderByDescending((m) => m.CreateTime);
            }
            else
            {
                messages = (from Message m in msgDB.Messages
                            where
                                m.Title.Contains(searchText) ||
                                m.Content.Contains(searchText) ||
                                (m.Contact != null &&
                                (m.Contact.DisplayName.Contains(searchText) ||
                                m.Contact.PhoneNumber.Contains(searchText)))
                            select m).OrderByDescending((m)=>m.CreateTime);
            }

            foreach (var m in messages)
            {
                this.Items.Add(new ItemViewModel(m));
            }
            
            this.IsDataLoaded = true;
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (null != handler)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        internal void DeleteMessage(ItemViewModel item)
        {
            Items.Remove(item);
            msgDB.Messages.DeleteOnSubmit(item.Message);
            msgDB.SubmitChanges();
        }

        internal void Search(string p)
        {
            //throw new NotImplementedException();
            LoadData(p);
        }
    }
}